/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QLNV.Action;

import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.LookupDispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import java.util.*;

import QLNV.Bean.*;
import QLNV.Entity.*;

/**
 *
 * @author Ice
 */
public class PhongBanAction extends LookupDispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String VIEW = "view";

    /** Provides the mapping from resource key to method name.
     * @return Resource key / method name map.
     */
    @Override
    protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("phongban.insert", "insert");
        map.put("phongban.update", "update");
        map.put("phongban.delete", "delete");
        map.put("phongban.search", "search");
        map.put("phongban.View", "view");
        //map.put("phongban.select", "select");
        return map;
    }

    /**
     * Action called on View button click
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception 
     */
    public ActionForward view(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        String errorMsg = "";
        PhongBanForm pBanForm = (PhongBanForm) form;
        ArrayList<PhongBan> pBanList = new ArrayList<PhongBan>();
        pBanList = pBanForm.view();
        request.setAttribute("pBanList", pBanList);
        request.setAttribute("errorMessage", errorMsg);
        return mapping.findForward(VIEW); //forward to PhongBanInfo.jsp
    }

    /** Action called on Insert button click
     */
    public ActionForward insert(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        response.setContentType("text/html; charset=UTF-8");
        ArrayList<PhongBan> pBanList = new ArrayList<PhongBan>();
        PhongBanForm pBanForm = (PhongBanForm) form;
        //PhongBan pBan = new PhongBan();
        String maPban = pBanForm.getMapban();
        String tenPban = pBanForm.getTenpban();
        String message = pBanForm.insert(maPban, tenPban);
        pBanList = pBanForm.view();
        request.setAttribute("errorMessage", message);
        request.setAttribute("pBanList", pBanList);
        return mapping.findForward(VIEW);
    }

    /** Action called on Edit button click
     */
    public ActionForward update(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws SQLException {
        PhongBanForm pBanForm = (PhongBanForm) form;
        ArrayList<PhongBan> pBanList = new ArrayList<PhongBan>();
        int row = pBanForm.update(pBanForm.getMapban(), pBanForm.getTenpban());
        if (row != 0) {
            request.setAttribute("errorMessage", "Updated Successfully");
        }
        pBanList = pBanForm.view();
        request.setAttribute("pBanList", pBanList);
        return mapping.findForward(VIEW);
    }

    /** Action called on Delete button click
     */
    public ActionForward delete(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        ArrayList<PhongBan> pBanList = new ArrayList<PhongBan>();
        PhongBanForm pBanForm = (PhongBanForm) form;
        String maPban = pBanForm.getMapban();
        String message = pBanForm.delete(maPban);
        //if(cnt!=0){
        pBanList = pBanForm.view();
        request.setAttribute("pBanList", pBanList);
        request.setAttribute("errorMessage", message);
        //}
        return mapping.findForward(VIEW);
    }

    /**
     * Search
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception 
     */
    public ActionForward search(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        ArrayList<PhongBan> pBanList = new ArrayList<PhongBan>();
        PhongBanForm pBanForm = (PhongBanForm) form;
        //PhongBan pBan = new PhongBan();
        String maPban = pBanForm.getMapban();
        pBanList = pBanForm.search(maPban);
        request.setAttribute("pBanList", pBanList);
        if(!pBanList.isEmpty()){
            request.setAttribute("errorMessage", "1");
        }
        else {
            request.setAttribute("errorMessage", "Không có ai");
        }
        return mapping.findForward(VIEW);
    }
    /* And your JSP would have the following format for submit buttons:
    
    <html:form action="/test">
    <html:submit property="method">
    <bean:message key="button.add"/>
    </html:submit>
    <html:submit property="method">
    <bean:message key="button.edit"/>
    </html:submit>
    <html:submit property="method">
    <bean:message key="button.delete"/>
    </html:submit>
    </html:form>
     */
}
